package com.yy.main.app.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yy.kit.collections.YYMapKits;
import com.yy.kit.string.YYStrKits;
import com.yy.main.app.mapper.TRoleMapper;
import com.yy.main.app.mapper.TRolePermissionMapper;
import com.yy.main.app.service.IRoleService;
import com.yy.pojo.base.TRole;
import com.yy.pojo.base.TRolePermission;
import com.yy.pojo.base.TRolePermissionExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 杨阳
 * @since 2017-08-22
 */
@Service("iRoleService")
public class RoleServiceImpl implements IRoleService {

    @Autowired
    private TRoleMapper tRoleMapper;
    @Autowired
    private TRolePermissionMapper tRolePermissionMapper;

    @Override
    public PageInfo roleTable(Integer page, Integer limit, String name) {
        PageHelper.startPage(page, limit);
        return new PageInfo(tRoleMapper.roleTable(name));
    }

    @Override
    @Transactional
    public Map<String, Object> saverole(TRole role, String permissionIds) {
        Map<String, Object> result = YYMapKits.createHashMap();
        if (null == permissionIds || "".equals(permissionIds)) {
            result.put("success", Boolean.FALSE);
            result.put("msg", "请选择角色关联的权限!");
            return result;
        }

        // 删除掉原有角色关联的权限
        delRolePermissionByRoleId(role.getId());

        if (YYStrKits.isBlank(role.getId()) || "-1".equals(role.getId())) {// 增加
            role.setId(YYStrKits.getTableId());
            Integer b = tRoleMapper.insert(role);
            if (b > 0) {
                result.put("success", Boolean.TRUE);
                // 插入角色与权限关联关系
                insertRolePermission(role.getId(), permissionIds);
                result.put("msg", "增加角色成功!!");
            } else {
                result.put("success", Boolean.FALSE);
                result.put("msg", "增加角色失败，请重试!!");
            }
        } else {// 修改
            Integer b = tRoleMapper.updateByPrimaryKey(role);
            if (b > 0) {
                // 插入角色与权限关联关系
                insertRolePermission(role.getId(), permissionIds);
                result.put("success", Boolean.TRUE);
                result.put("msg", "修改角色成功!!");
            } else {
                result.put("success", Boolean.FALSE);
                result.put("msg", "修改角色失败，请重试!!");
            }
        }

        return result;
    }

    /**
     * <p>
     * 插入角色与权限的关联关系
     * </p>
     *
     * @param role_id       角色id
     * @param permissionIds 权限id字符串，以逗号分隔
     * @author 杨阳
     * @since 2017年9月14日
     */
    private void insertRolePermission(String role_id, String permissionIds) {
        String[] ids = permissionIds.split(",");
        for (String permissionsId : ids) {
            TRolePermission rp = new TRolePermission();
            rp.setId(YYStrKits.getTableId());
            rp.setPermissions_id(permissionsId);
            rp.setRole_id(role_id);
            tRolePermissionMapper.insert(rp);
        }
    }

    /**
     * <p>
     * 根据角色id，删除掉与角色相关联的权限
     * </p>
     *
     * @param role_id 角色id
     * @author 杨阳
     * @since 2017-9-14
     */
    private void delRolePermissionByRoleId(String role_id) {
        // 删除掉原有角色关联的权限
        TRolePermissionExample example = new TRolePermissionExample();
        example.createCriteria().andRole_idEqualTo(role_id);
        tRolePermissionMapper.deleteByExample(example);
    }

    @Override
    public List<TRole> userOwnRoleList(String userid) {
        return tRoleMapper.userOwnRoleList(userid);
    }

}
